home *** CD-ROM | disk | FTP | other *** search
Lex Description | 1995-06-12 | 1.1 KB | 75 lines |
- %{
- #include <stdio.h>
-
- #define YYSTYPE double
-
- static int prec = 0;
- static YYSTYPE mem = 0;
- %}
-
- %start list
-
- %token NUMBER
-
- %left '+' '-'
- %left '*' '/'
- %left '%'
- %left UMINUS
-
- %%
-
- list: /* empty */
- | list '\n'
- | list error '\n' { yyerrok; }
- | list expr '\n' { printf( "%.*f\n", prec, $2 ); fflush( stdout ); }
- | list asgn '\n'
- ;
-
- expr: '-' expr %prec UMINUS { $$ = -$2; }
- | expr '+' expr { $$ = $1 + $3; }
- | expr '-' expr { $$ = $1 - $3; }
- | expr '*' expr { $$ = $1 * $3; }
- | expr '/' expr { $$ = $1 / $3; }
- | expr '%' expr { $$ = $1 * ($3 / 100); }
- | 'R' { $$ = mem; }
- | NUMBER
- ;
-
- asgn: 'P' NUMBER { prec = $2; }
- | 'A' NUMBER { mem += $2; }
- | 'S' NUMBER { mem -= $2; }
- | 'C' { mem = 0.0; }
- ;
- %%
-
- void yyerror( char *s )
- {
- }
-
- int yylex( void )
- {
- int c;
-
- while ( (c = getchar( )) == ' ' || c == '\t' )
- ; /* skip white spaces */
-
- if ( c == EOF )
- return 0;
-
- if ( c == '.' || isdigit( c )) {
- ungetc( c, stdin );
- scanf( "%lf", &yylval );
- return NUMBER;
- }
-
- return c;
- }
-
- void main( )
- {
- while ( !feof( stdin ))
- yyparse( );
-
- exit( 0 );
- }
-